#include "subject_mannager.h"

void drawAddSubject()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------------------------------------------" << std::endl;
    std::cout << "|                                      Subject Manage System                                   |" << std::endl;
    std::cout << "------------------------------------------------------------------------------------------------" << std::endl;
    std::cout << "|                                           Add Subject                                        |" << std::endl;
    std::cout << "------------------------------------------------------------------------------------------------" << std::endl;
    std::cout << "| \033[01;32mNew Subject SN \033[01;35m| \033[01;32mNew Subject Title \033[01;35m| \033[01;32mNew Subject Nature \033[01;35m| \033[01;32mTotal Classes \033[01;35m| \033[01;32mCredits \033[01;35m| \033[01;32mRemained \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------------------------------------------" << std::endl;
    std::cout << "|:               |:                  |:                   |:              |:        |:         |" << std::endl;
    std::cout << "------------------------------------------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                                                    |" << std::endl;
    std::cout << "------------------------------------------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);
}

void drawDeleteSubject()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------" << std::endl;
    std::cout << "|                 Subject Manage System                    |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|                      Delete Subject                      |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mSN:                                                       \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                |" << std::endl;
    std::cout << "------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);
}

void drawModifySubject1()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------" << std::endl;
    std::cout << "|                 Subject Manage System                    |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|               Modify Subject Information                 |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mSubject SN:                                               \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                |" << std::endl;
    std::cout << "------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);    
    moveCursor('r', 12);
}

int drawModifySubject2(std::string sn)
{
    system("clear");
    bool isok = 0;
    int index;
    std::vector< std::vector<std::string> >  tempfile = readFile(SUB_PATH);
    std::vector< std::vector<std::string> >  file(6), input(6);
    file[0].push_back("SN");
    file[1].push_back("Subject Title");
    file[2].push_back("Subject Nature");
    file[3].push_back("Total Classes");
    file[4].push_back("Credits");
    file[5].push_back("Remained");
    
    input[0].push_back("SN");
    input[1].push_back("Subject Title");
    input[2].push_back("Subject Nature");
    input[3].push_back("Total Classes");
    input[4].push_back("Credits");
    input[5].push_back("Remained");

    for(int i = 0; i < 6; i++) input[i].push_back("");


    tempfile.erase(tempfile.begin() + 5);

    for(int i = 0; i < tempfile.size(); i++)
    {
        tempfile[i].erase(tempfile[i].begin());
    }
    for(int i = 0; i < tempfile[0].size(); i++)
    {
        if(tempfile[0][i] == sn)
        {
            for(int j = 0; j < tempfile.size(); j++)
            {
                file[j].push_back(tempfile[j][i]);
            }
            index = i;
            isok = true;
            break;
        }
        
    }
    if(!isok) return false;
    tempfile = readFile(SUB_PATH);
    for(int i = 0; i < tempfile.size(); i++)
    {
        tempfile[i].erase(tempfile[i].begin());
    }
    int max_[6] = {0};
    int _max = 1;
    for(int i = 0; i < 6; i++)
    {
        max_[i] = file[i][0].size() > file[i][1].size() ? file[i][0].size() : file[i][1].size();
        max_[i] += 10;
        _max += max_[i] + 1;
    }

    std::vector<std::string> title_list_1, title_list_2, title_list_3;
    title_list_1.push_back("Subject Manage System");
    title_list_1.push_back("Modify Subject Information");
    title_list_1.push_back("");
    title_list_1.push_back("Old Subject Information");

    title_list_2.push_back("");
    title_list_2.push_back("New Subject Information");

    title_list_3.push_back("Press Enter to continue...");
    for (int i = 0; i < title_list_1.size(); i++)
    {

        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
        std::cout << "\033[01;35m|\033[0m";

        for (int j = 0; j < ((_max - 2) / 2) - (title_list_1[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m" << title_list_1[i] << "\033[0m";

        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list_1[i].size() - title_list_1[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }
    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    for (int i = 0; i < file[0].size(); i++)
    {
        for(int j = 0; j < file.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (file[j][i].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << file[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (file[j][i].size() - file[j][i].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;

        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }

    for (int i = 0; i < title_list_2.size(); i++)
    {

        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
        std::cout << "\033[01;35m|\033[0m";

        for (int j = 0; j < ((_max - 2) / 2) - (title_list_2[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m" << title_list_2[i] << "\033[0m";

        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list_2[i].size() - title_list_2[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }
    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    for (int i = 0; i < input[0].size(); i++)
    {
        for(int j = 0; j < input.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (input[j][i].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << input[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (input[j][i].size() - input[j][i].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;

        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }

    for (int i = 0; i < title_list_3.size(); i++)
    {
        std::cout << "\033[01;35m|\033[0m";

        for (int j = 0; j < ((_max - 2) / 2) - (title_list_3[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m" << title_list_3[i] << "\033[0m";

        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list_3[i].size() - title_list_3[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }
    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    std::cout << "\033[01;32m";
    int deviation = 1;
    for(int i = 0; i < 6; i++)
    {
        moveCursor('u', (i == 0) ? 4 : 1);
        moveCursor('r', deviation);
        if(i >= 5)
        {
            tempfile[i + 1][index] = readCommand();
            if(tempfile[i + 1][index] == "q") return -1;
        }
        else
        {
            tempfile[i][index] = readCommand();
            if(tempfile[i][index] == "q") return -1;
        }
        deviation += max_[i] + 1;
    }
    std::cout << "\033[0m";

    for(int i = 0; i < 2; i++)
    {
        std::map<std::string, bool> mp;
        for(int j = 0; j < tempfile[0].size(); j++)
        {
            if(mp[tempfile[i][j]] == false) mp[tempfile[i][j]] = true;
            else
            {
                return false;
            }
        }
    }
    for(int i = 0; i < tempfile[2].size(); i++)
    {
        if(tempfile[2][i] != "Required" && tempfile[2][i] != "Elective") return false;
    }
    std::fstream f;
    f.open(SUB_PATH, std::ios::out);
    for(int i = 0; i < tempfile[0].size(); i++)
    {
        std::string temp;
        for(int j = 0; j < tempfile.size(); j++)
        {
            temp.append("\"" + tempfile[j][i] + "\"");
            if(j != tempfile.size() - 1) temp.append(" ");
        }
        f << temp << std::endl;
    }

    f.close();

    return true;
}

void drawSpecificSubject()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------" << std::endl;
    std::cout << "|                 Subject Manage System                    |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|                 Check Specific Subject                   |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mSubject SN:                                               \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                |" << std::endl;
    std::cout << "------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);    
    moveCursor('r', 12);
}

int showSpecificSubject()
{
    bool isok = 0;

    drawSpecificSubject();
    std::string sn = readCommand();
    if(sn == "q") return -1;
    std::string subject_title;
    if(sn.empty()) return false;
    std::vector<std::string> ans;
    std::fstream file;
    file.open(SUB_PATH, std::ios::in);
    while(!file.eof())
    {
        std::string line;
        std::vector<std::string> temp;
        getline(file, line);
        temp = split(line, ' ');
        if(temp[0] == sn)
        {
            isok = 1;
            ans = temp;
            subject_title = temp[1];
            break;
        }
    }
    if(isok)
    {
        system("clear");
        std::vector<std::string> label_list, title_list;
        label_list.push_back("SN");
        label_list.push_back("Subject Title");
        label_list.push_back("Subject Nature");
        label_list.push_back("Total Classes");
        label_list.push_back("Credits");
        label_list.push_back("Selected");
        label_list.push_back("Remained");
        title_list.push_back("Subject Manage System");
        title_list.push_back("Subject " + subject_title);
        int max_[7];
        for(int i = 0; i < 7; i++) max_[i] = std::max(label_list[i].size(), ans[i].size()) + 6;
        int _max = 1;
        for(int i = 0; i < 7; i++) _max += max_[i] + 1;
        for (int i = 0; i < title_list.size(); i++)
        {

            for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
            std::cout << std::endl;

            std::cout << "\033[01;35m|\033[0m";
            for (int j = 0; j < ((_max - 2) / 2) - (title_list[i].size() / 2); j++)
                std::cout << " ";
            std::cout << "\033[01;35m" << title_list[i] << "\033[0m";
            for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list[i].size() - title_list[i].size() / 2); j++)
                std::cout << " ";
            std::cout << "\033[01;35m|\033[0m" << std::endl;
        }
        for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;

        for(int j = 0; j < 7; j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (label_list[j].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << label_list[j] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (label_list[j].size() - label_list[j].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;

        for(int j = 0; j < 7; j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (ans[j].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << ans[j] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (ans[j].size() - ans[j].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;

        std::string tail = "Press Enter To Go Back...";
        std::cout << "\033[01;35m|";
        std::cout << tail;
        for(int i = 0; i < (_max - 2 - tail.size()); i++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
        for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
        char *p = getpass("");
        return isok;
    } 
    return isok;
}

int addSubject()
{
    drawAddSubject();
    moveCursor('r',2);
    std::string sn, title, nature, total, credit, remained;
    std::cout << "\033[01;32m";
    sn = readCommand();
    if(sn == "q") return -1;
    moveCursor('u', 1);
    moveCursor('r',19);
    title = readCommand();
    if(title == "q") return -1;
    moveCursor('u', 1);
    moveCursor('r',39);
    nature = readCommand();
    if(nature == "q") return -1;
    moveCursor('u', 1);
    moveCursor('r',60);
    total = readCommand();
    if(total == "q") return -1;
    moveCursor('u', 1);
    moveCursor('r',76);
    credit = readCommand();
    if(credit == "q") return -1;
    moveCursor('u', 1);
    moveCursor('r',86);
    remained = readCommand();
    if(remained == "q") return -1;
    std::cout << "\033[0m";
    if(isDigit(sn) == -1 || isDigit(credit) == -1 || isDigit(credit) == -1 || isDigit(remained) == -1) return 0;
    if(Sub_isValid(title, sn))
    {
        std::fstream file;
        file.open(SUB_PATH, std::ios::out|std::ios::app);
        std::string addline;
        addline.append("\"");
        addline.append(sn);
        addline.append("\" \"");
        addline.append(title);
        addline.append("\" \"");
        addline.append(nature);
        addline.append("\" \"");
        addline.append(total);
        addline.append("\" \"");
        addline.append(credit);

        addline.append("\" \"0\" \"" + remained + "\"");
        file << addline << std::endl;
        //file.close();
        return 1;
    }
    else return 0;
}

int deleteSubject()
{
    bool isok = 0;
    drawDeleteSubject();
    moveCursor('r', 4);
    std::cout << "\033[01;32m";
    std::string sn;
    sn = readCommand();
    if(sn == "q") return -1;
    std::cout << "\033[0m";
    std::fstream in, out;
    in.open(SUB_PATH);
    std::vector<std::string> ret;
    while(!in.eof())
    {
        std::vector<std::string> temp;
        std::string line;
        getline(in, line);
        if(line.empty()) continue;
        temp = split(line, ' ');
        if(temp[0] == sn)
        {
            isok = 1;
        }
        else ret.push_back(line);
    }
    if(isok)
    {
        out.open(SUB_PATH, std::ios::out);
        for(auto a : ret) out << a << std::endl;
    }
    return isok;
}

int modifySubject()
{
    drawModifySubject1();
    std::string sn;
    sn = readCommand();
    if(sn == "q") return -1;
    if(sn.empty()) return false;
    return drawModifySubject2(sn);
}

bool Sub_isValid(std::string title, std::string sn)
{
    std::fstream file;
    std::string temp;
    file.open(SUB_PATH, std::ios::in);
    while(!file.eof()) 
    {
        getline(file, temp);
        std::vector<std::string> lines = split(temp, ' ');
        if(lines[0] ==  sn || lines[1] == title) return 0;
    }
    return 1;
}